import {getYMD,getNowFormatDate,getDay,formatGMTDate,sortBy} from '../../../../assets/js/util.js' 

import echarts from 'echarts'

export default{
  data() {
    return{
      // 默认参数 和 URL
      SRC: '',
      DP: {
        p: '',
        f: '',
        uid: ''
      },
      prop: '',
      order: '',
      pickerOptions: {
        shortcuts: [{
          text: '近7天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '近15天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '近30天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      loading: false,
      searchable: false,
      title:'广告日数据',
      xlsxTitle:'',
      ruleForm :{
        sday:'',
        eday:'',
        startDate: '',
        attr:'',
        platForm: '',
        agent: '',
        settlWay: '',
        channel: '',
        adPlace:''
      },
      tableHead1 :[
        {
          "prop":"dateStr",
          "label":"日期"
        },
        {
          "prop":"clkEquipment",
          "label":"点击设备"
        },
        {
          "prop":"actEquipment",
          "label":"激活设备"
        },
        {
          "prop":"newActive",
          "label":"实际激活"
        },
        {
          "prop":"newEquipment",
          "label":"新增设备"
        },
        {
          "prop":"newAccount",
          "label":"新增账号"
        },
        {
          "prop":"newPay",
          "label":"新增付费金额"
        },
        {
          "prop":"payrate",
          "label":"付费率"
        },
        {
          "prop":"discount",
          "label":"折后成本"
        },
        {
          "prop":"cpa",
          "label":"CPA"
        },
        {
          "prop":"totalpay",
          "label":"累计充值【90日】"
        },
        {
          "prop":"roi",
          "label":"渠道后ROI(%)"
        },
      ],
      tableDataAll:[],
      tableDataAllZ:[],
      tableData1:[],
      tableData2: [],
      tableData3: [],
      /*当前页显示数量*/
      pageSize: 10,
      /*每页显示的数量*/
      pageSizes: [10, 20, 50, 100],
      /*dataTotal表数量*/
      dataTotal: 0,
      /*分页器*/
      currentPage: 1,
      // 归属
      attrs:[],
      // 渠道名称
      channels:[],
      // 代理商
      agents:[],
      // 平台
      platForms:[],
      // 结算方式
      settlWays:[],
      // 广告位置
      adPlaces:[]
    }
  }, 
  methods: {
    // 通用 成功 与 失败
    successFun(data) {
      this.$message({
        message: data,
        type: 'success'
      });
    },
    errorFun(data) {
      this.$message.error(data);
    },
    dateChange(val){
      if(val){
        var val = val.split(" ");
        this.sday = val[0];
        this.eday = val[2];
      }
    },
    agentChange(){

    },
    attrChange(){

    },
    channelChange(){

    },
    settlWayChange(){

    },
    adPlaceChange(){

    },
    loads() {
        this.loading = !this.loading; //加载中
        this.searchable = !this.searchable; //查询按钮不可用
    },
    search(){
      this.loads(); //加载 查询按钮不可用
      this.apiAjax(this.sday,this.eday)

      
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.tableDataAllZ = this.tableDataAll.slice();
      this.tableDataAllZ = sortBy(this.tableDataAllZ,this.prop,this.order);
      this.tableData2 = [];
      this.tableData2 =this.tableDataAllZ.slice((this.currentPage-1)*(this.pageSize),this.currentPage*this.pageSize);
      this.dataTotal = this.tableDataAllZ.length;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.tableDataAllZ = this.tableDataAll.slice();
      this.tableDataAllZ = sortBy(this.tableDataAllZ,this.prop,this.order);
      this.tableData2 = [];
      this.tableData2 =this.tableDataAllZ.slice((this.currentPage-1)*(this.pageSize),this.currentPage*this.pageSize);
      this.dataTotal = this.tableDataAllZ.length;
    },
    edit (){

    },
    handleDelete (){

    },
    // arrTojs
    getArrJson(arr){
      var arrNew = [];
      for(var i=0;i<arr.length;i++){
        var obj ={};
        obj.id = arr[i];
        obj.agentName = arr[i];
        arrNew.push(obj);
      }
      return arrNew;
    },
    // 获取选择信息
    getData(){
      this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=initParam`,{
        params: {'ps':`uid=${this.DP.uid}`}
      }).then(function(res){
         var data =  JSON.parse(JSON.parse(res.body));
        if(data.res == "0"){
          this.agents = this.getArrJson(data.agent);
          this.platForms = this.getArrJson(data.platform);
          this.adPlaces = this.getArrJson(data.advPosition);
          this.settlWays = this.getArrJson(data.paytype);
          this.channels = this.getArrJson(data.channelName);
          this.attrs = this.getArrJson(data.affiliation);  
        }else{
           this.errorFun(data['msg'])
        }
      }).catch(function(res){
        console.log(res)
      })
    },
    findData (data){
      this.sday = formatGMTDate(new Date(new Date().getTime() - 1000*60*60*24*1),true,true);
      this.eday = formatGMTDate(new Date(new Date().getTime() - 1000*60*60*24*1),true,true);
      this.ruleForm.startDate = [this.sday,this.eday];
      this.loads();
      this.apiAjax(this.sday,this.eday)
    },
    apiAjax(sday,eday){
      this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`,{
        params: {'ps':`sday=${sday}&eday=${eday}&agent=${this.ruleForm.agent}&platform=${this.ruleForm.platForm}&affiliation=${this.ruleForm.attr}&channelName=${this.ruleForm.channel}&paytype=${this.ruleForm.settlWay}&advPosition=${this.ruleForm.adPlace}&uid=${this.DP.uid}`}
      }).then(function(res){
        this.loads();
        var data = JSON.parse(JSON.parse(res.body));
        if(parseInt(data['res']) === 0){
          this.tableData3 = data.Totals;
          this.tableDataAll = data.activeTranDatas;
          this.tableDataAllZ = this.tableDataAll.slice();
          if(this.tableDataAll.slice().length>0){
            this.tableData2 = [];
            this.tableData2 =this.tableDataAllZ.slice((this.currentPage-1)*(this.pageSize),this.currentPage*this.pageSize);
            this.dataTotal = this.tableDataAllZ.length;
          }
        }else{
          this.errorFun(data['msg'])
        }
      }).catch(function(res){})
    },
    sortChage({ column, prop, order }){
      this.prop = prop;
      this.order = order;
      if(this.tableDataAll.slice().length>0){
        this.tableDataAllZ = this.tableDataAll.slice();
        this.tableDataAllZ = sortBy(this.tableDataAllZ,this.prop,this.order);
        this.currentPage = 1;
        this.tableData2 = [];
        this.tableData2 =this.tableDataAllZ.slice((this.currentPage-1)*(this.pageSize),this.currentPage*this.pageSize);
        this.dataTotal = this.tableDataAllZ.length;
      }
    },
    // 表格下载
    excel(tableIndex,sheet) {
      var that = this;
      that.xlsxTitle="广告日数据-"+that.sday+'-'+that.eday;
      that.downloadExl(that["tableHead1"],that["tableDataAllZ"],that.xlsxTitle,sheet);
    },
    getSummaries(param){
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        sums[0] = this.tableData3.dateStr;
        sums[1] = this.tableData3.clkEquipment;
        sums[2] = this.tableData3.actEquipment;
        sums[3] = this.tableData3.newActive;
        sums[4] = this.tableData3.newEquipment;
        sums[5] = this.tableData3.newAccount;
        sums[6] = this.tableData3.newPay;
        sums[7] = this.tableData3.payrate;
        sums[8] = this.tableData3.discount;
        sums[9] = this.tableData3.cpa;
        sums[10] = this.tableData3.totalpay;
        sums[11] = this.tableData3.roi;
      });
      return sums;
    }
  },
  computed:{
		// xlsx(){
		//   return this.title+'.xlsx'
		// }
	},
  mounted() {
    // 初始化URL
    this.SRC = this.$store.getters.src;
    this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
    this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
    this.DP.uid = localStorage.getItem('ZULONGBIUSERID');
    this.getData();
    this.findData(0);
    this.$nextTick(function(){
      document.querySelector("#scrollAW").style.width = document.querySelector("#scrollAW .el-table__body").style.width
    })
  }
}